/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Reflection;
using log4net;

namespace OpenServers.Net.Common.Utils {
    /// <summary>
    /// wrapper object allowing easy access to reflection methods.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PrivateObject<T> : PrivateStaticObject {
        private static readonly ILog log = LogManager.GetLogger(typeof(PrivateObject<T>));

        private readonly T _wrappedObject;

        /// <summary>
        /// constructor.
        /// </summary>
        public PrivateObject(T wrappedObject)
            : base(wrappedObject.GetType()) {
            this._wrappedObject = wrappedObject;
        }

        /// <summary>
        /// The wrapped object.
        /// </summary>
        public T WrappedObject {
            get { return _wrappedObject; }
        }

        /// <summary>
        /// executes a method.
        /// </summary>
        public object ExecuteMethod(string methodName, params object[] parameters) {
            Type[] parameterTypes;
            try {
                parameterTypes = ReflectionUtils.GetTypes(parameters);
            } catch (ArgumentNullException) {
                // a parameter was null, lets try to infer the type.
                MethodInfo[] methodInfos = GetMethods(methodName, parameters.Length);
                if (methodInfos.Length == 1) {
                    return CallMethod(methodInfos[0], parameters);
                } else {
                    throw;
                }
            }
            return ExecuteMethod(methodName, parameterTypes, parameters);
        }

        /// <summary>
        /// executes a method.
        /// </summary>
        public object ExecuteMethod(string methodName, Type[] parameterTypes, object[] parameters) {
            MethodInfo methodInfo = GetMethod(methodName, parameterTypes);
            if (methodInfo == null) {
                throw new MethodAccessException(string.Format("method [{0}] with parameters [{1}] not found", methodName, ArrayUtils.Join(parameterTypes)));
            }
            return CallMethod(methodInfo, parameters);
        }

        private object CallMethod(MethodInfo methodInfo, object[] parameters) {
            try {
                return methodInfo.Invoke(WrappedObject, parameters);
            } catch (TargetInvocationException ex) {
                log.Error("TargetInvocationException", ex);
                throw ex.InnerException;
            }
        }
    }
}
